package com.ucw.mountain.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static int getCurrentTimeSeconds() {
        return (int) (System.currentTimeMillis() / 1000);
    }

    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static long getDiff(String lastModifyTime) {
        long diff = System.currentTimeMillis() - TypeConverter.str2Long(lastModifyTime);
        return (diff / 1000);
    }

    public static Integer getDiffMinites(Integer lastModifySeconds) {
        Integer diffSeconds = getCurrentTimeSeconds() - lastModifySeconds;
        return (diffSeconds / 60);
    }

    public static Integer getDiffMinites(Integer startModifySeconds, Integer endModifySeconds) {
        Integer diffSeconds = endModifySeconds - startModifySeconds;
        return (diffSeconds / 60);
    }

    /**
     * 获取今日零点时间戳（单位：秒）
     * 
     * @return
     */
    public static Integer getTodayStartTimeSeconds() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    public static long getTodaySpecifyTime(int hour, int min, int sec) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, sec);
        return cal.getTimeInMillis();
    }

    /**
     * 获取某年某月第一天零点时间戳（单位：秒）
     * 
     * @param year
     * @param month
     * @return
     */
    public static Integer getFistDayTime(Short year, Byte month) {
        if (year == null || month == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, cal.getMinimum(Calendar.DATE), 0, 0, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取某年某月的天数
     * 
     * @param year
     * @param month
     * @return
     */
    public static Integer getDays(Short year, Byte month) {
        if (year == null || month == null) {
            return null;
        }

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONDAY, month - 1);
        return cal.getActualMaximum(Calendar.DATE);
    }

    public static boolean isDifferentDay(Long startTime) {
        Calendar dayOne = Calendar.getInstance();
        dayOne.setTimeInMillis(startTime);
        int dayOfYearOne = dayOne.get(Calendar.DAY_OF_YEAR);

        Calendar dayTwo = Calendar.getInstance();
        int dayOfYearTwo = dayTwo.get(Calendar.DAY_OF_YEAR);
        return (dayOfYearOne != dayOfYearTwo);
    }

    /**
     * 获取指定日期当天的开始时间的时间戳（单位：秒）
     * 
     * @return
     */
    public static int getStartTimeSeconds(int year, int month, int date) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取指定日期当天的结束时间的时间戳（单位：秒）
     * 
     * @return
     */
    public static int getEndTimeSeconds(int year, int month, int date) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取当天结束时间
     * 
     * @return
     */
    public static int getTodayEndTimeSeconds() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取当前前后某一天的开始时间
     * 
     * @param num
     * @return
     */
    public static int getOtherDayStartTimeSecondes(int num) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + num);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取当前前后某一天的开始时间
     * 
     * @param num
     * @return
     */
    public static int getOtherDayEndTimeSecondes(int num) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + num);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 获取当前前后某一天的8位整数格式
     * 
     * @param day
     * @return
     */
    public static int getOtherDayYMDInteger(int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + day);
        return (cal.get(Calendar.YEAR)) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取当前前后某一月的第一天的8位整数格式
     * 
     * @param month
     * @return
     */
    public static int getOtherMonthFirstDayYMDInteger(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
        return (cal.get(Calendar.YEAR)) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + 1;
    }

    /**
     * 获取当前前后某一月的6位整数格式
     * 
     * @param month
     * @return
     */
    public static int getOtherMonthYMInteger(int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month);
        return (cal.get(Calendar.YEAR)) * 100 + (cal.get(Calendar.MONTH) + 1);
    }

    /**
     * 把年月日时分秒 时间戳 转 yyyyMMddHHmmss格式
     * 
     * @param time
     * @return
     */
    public static String formatyyyyMMddHHmmss(Long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(time * 1000);
    }

    /**
     * 把年月日时分秒 时间戳 转 yyyyMMddHHmmss格式 "2017-01-02 10:09", "yyyy-MM-dd HH:mm"
     * 
     * @param time
     * @return
     */
    public static String formatyyyyMMddHHmm(Long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(time * 1000);
    }

    public static void main(String[] args) {
        long l = 1488608460;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        System.out.println(l * 1000);
        System.out.println(System.currentTimeMillis());
        System.out.println(sdf.format(l));
        System.out.println(sdf.format(System.currentTimeMillis()));
    }

    public static Date formatTimeStamp(String timeStamp) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            return format.parse(format.format(Long.valueOf(timeStamp) * 1000));
        } catch (ParseException e) {
            return null;
        }
    }

    public static int parseDate2Int(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return (int) (calendar.getTimeInMillis() / 1000);
    }

    /**
     * 把年月日时分秒 时间戳 转 yyyyMMddHHmmss格式
     * 
     * @param time
     * @return
     */
    public static String formatyyyyMMdd(Long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(time * 1000);
    }

    /**
     * 根据时间戳 转 date str；
     * 
     * @param timeStamp
     * @return
     */

    public static String getDateStr(Long timeStamp, String type) {
        if (timeStamp == null) {
            return "";
        }
        // yyyy-MM-dd hh:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        String date = sdf.format(new Date(timeStamp * 1000));
        return date;
    }

    /**
     * 当前时间转换为：yyMMddhhmmssSSS
     * 
     * @param timeStamp
     * @return
     */
    public static String getDateStrMilliSecond() {
        // yyMMddhhmmssSSS
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmssSSS");
        String date = sdf.format(new Date());
        return date;
    }

    /**
     * 获取指定日期的开始时间
     * 
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getOtherDayStartTimeSecondes(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return (int) (cal.getTimeInMillis() / 1000);
    }

    /**
     * 根据时间戳获取当前月份
     * 
     * @param timestamp
     * @return
     */
    public static int getMonth(Long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("MM");
        return Integer.parseInt(format.format(timestamp * 1000));
    }

    /**
     * 根据事件戳获取单前日期
     * 
     * @param timestamp
     * @return
     */
    public static int getDay(Long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("dd");
        return Integer.parseInt(format.format(timestamp * 1000));
    }

    /**
     * 根据事件戳获取单前日期
     * 
     * @param timestamp
     * @return
     */
    public static int getYear(Long timestamp) {
        SimpleDateFormat format = new SimpleDateFormat("YYYY");
        return Integer.parseInt(format.format(timestamp * 1000));
    }

    /**
     * 获取当前日期与传入时间戳相差的天数
     * 
     * @param first
     * @return
     */
    public static int getDaysBetweenDates(long first) {
        int repayTime = getStartTimeSecondsByTimestamp(first);
        return (getCurrentTimeSeconds() - repayTime) / (60 * 60 * 24);
    }

    /**
     * 根据时间戳获取当天开始的时间戳
     * 
     * @param timestamp
     * @return
     */
    public static int getStartTimeSecondsByTimestamp(long timestamp) {
        return getStartTimeSeconds(getYear(timestamp), getMonth(timestamp) - 1, getDay(timestamp));
    }

    public static int getAgeByCertId(String certId) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String year1 = simpleDateFormat.format(new Date());
        String year2 = certId.substring(6, 10);
        return Integer.parseInt(year1) - Integer.parseInt(year2);
    }
}
